package com.topc.code.utils;

import com.topc.code.judge.strategy.JudgeContext;
import com.topc.problem.model.code.JudgeInfo;
import com.topc.problem.model.enums.JudgeInfoMessageEnum;
import com.topc.problem.model.po.ProblemSample;

import java.math.BigDecimal;
import java.util.List;

public class StrategyUtils {
    public static JudgeContext getJudgeInfoResult(JudgeContext judgeContext,BigDecimal memory,BigDecimal time,BigDecimal needMemoryLimit, BigDecimal needTimeLimit){
        //获取输入输出内容
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        List<BigDecimal> timeList=judgeContext.getTimeList();
        List<BigDecimal> memoryList=judgeContext.getMemoryList();
        //获取题目配置信息
        List<ProblemSample> problemSampleList = judgeContext.getProblemSampleList();
        //设置初始状态
        JudgeInfoMessageEnum judgeInfoMessageEnums = JudgeInfoMessageEnum.ACCEPTED;
        //存入需要返回的结果信息(空间，时间消耗以及判题结果)
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);
        //判断输入和输出个数是否相同
        if (outputList.size() != inputList.size()) {
            judgeInfoMessageEnums = JudgeInfoMessageEnum.WRONG_ANSWER;
        }
        int sampleSize = problemSampleList.size();
        int score =0;
        int passResult;
        //判断每项输出和预期输出是否相等
        for ( passResult= 0; passResult < sampleSize&&passResult < timeList.size()&& passResult<memoryList.size(); passResult++) {
            //如果时间和空间有一个不符合
            ProblemSample problemSample = problemSampleList.get(passResult);
            String output = problemSample.getOutput();
            String result = outputList.get(passResult);
            result = result.replace("\r\n", "\n");
            result = result.replaceAll("(?m)^\\r?\\n|\\r?\\n$", "");
            output = output.replace("\r\n", "\n");
            output = output.replaceAll("(?m)^\\r?\\n|\\r?\\n$", "");
            System.out.println("结果:"+ output +"\n答案:"+ result);
            System.out.println("判定"+ output.equals(result));
            if (!output.equals(result)) {
                judgeInfoMessageEnums = JudgeInfoMessageEnum.WRONG_ANSWER;
                break;
            }else {
                //如果时间超时，则结果超时
                if(timeList.get(passResult).compareTo(needTimeLimit)>0){
                    judgeInfoMessageEnums = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
                    break;
                }else if (memoryList.get(passResult).compareTo(needMemoryLimit)>0){
                    judgeInfoMessageEnums = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
                    break;
                }
            }
        }
        //结算分数
//        double allScore=100f;
//        score = (int) (allScore * (float)passResult / (float)sampleSize);
        score = passResult*10;
        //检测完样例是否正确之后，判题目限制条件
        //判断内存
        if (memory.compareTo(needMemoryLimit)>0) {
            judgeInfoMessageEnums = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
        }
        //判断时间
        if (time.compareTo(needTimeLimit)>0) {
            judgeInfoMessageEnums = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
        }
        //返回结果
        judgeInfoResponse.setMessage(judgeInfoMessageEnums.getText());
        judgeContext.setJudgeInfo(judgeInfoResponse);
        judgeContext.setScore(score);
        return judgeContext;
    }

    public static void updateScore(){

    }
}
